﻿using System;
using System.Collections.Generic;
using UM_SERVER.Net;
using UM_SERVER.Packet.InPacket;
using System.Text;

namespace UM_SERVER.ProcessModel
{
    class ProcessInfomationModel
    {
        private static String PackageBody;
        private static String Protocol;

        /// <summary>
        ///     回馈获取自身信息成功到指定用户
        /// </summary>
        /// <param name="inpackage" type="UM_SERVER.Packet.InPacket.GetMySelfInfoPacket">
        ///     <para>
        ///         获取自身信息包
        ///     </para>
        /// </param>
        /// <param name="DIC_USERINFO" type="System.Collections.Generic.Dictionary<string,string>">
        ///     <para>
        ///         用户所有信息字典
        ///     </para>
        /// </param>
        public static void ACK_SucessfulGetMySelfInfo(GetMySelfInfoPacket inpackage, Dictionary<String, String> DIC_USERINFO)
        {
            PackageBody = "";
            int TotalSize = 0;
            byte[] packageBody = null;
            //向用户发送成功登陆回馈
            foreach (var pair in DIC_USERINFO)
            {
                if (pair.Key != "SEX" && pair.Key != "AGE" && pair.Key != "USERID" && pair.Key != "MESSION" && pair.Key != "LOGINLIVE")
                {
                    TotalSize += 4 + Encoding.Default.GetBytes(pair.Value).Length;
                }
                else
                {
                    TotalSize += 4;
                }
            }
            packageBody = new byte[TotalSize];
            int Offset = 0;
            foreach (var pair in DIC_USERINFO)
            {
                if (pair.Key != "SEX" && pair.Key != "AGE" && pair.Key != "USERID" && pair.Key != "MESSION" && pair.Key != "LOGINLIVE")
                {
                    byte[] IntLegnth = BitConverter.GetBytes(Encoding.Default.GetBytes(pair.Value).Length);
                    byte[] Value = Encoding.Default.GetBytes(pair.Value);
                    Array.ConstrainedCopy(IntLegnth, 0, packageBody, Offset, 4);
                    Offset += 4;
                    Array.ConstrainedCopy(Value, 0, packageBody, Offset, Value.Length);
                    Offset += Value.Length;
                }
                else
                {
                    byte[] IntLegnth = BitConverter.GetBytes(int.Parse(pair.Value));
                    Array.ConstrainedCopy(IntLegnth, 0, packageBody, Offset, 4);
                    Offset += 4;
                }
            }
            //得到协议
            byte[] protocol1 = ProtocolFactory.ProtocolFactory.GetProtocol("ReplyGetMySelfInfo",
                                                                          (int)Packet.Protocol.OUT_NetworkCommand.REPLY_GETMYSELFINFO_PACKAGE,
                                                                          (int)Packet.Protocol.NetworkProtocol.Base,
                                                                          packageBody,
                                                                          ProtocolFactory.ProtocolFactory.ACK.No,
                                                                          ProtocolFactory.ProtocolFactory.ENCRYPT.No, "");
            NetControl.Send(protocol1, inpackage.GetTalkPort(), inpackage.GetUserIpAddress());
        }

        /// <summary>
        ///     回馈获取用户分组信息请求
        /// </summary>
        /// <param name="inpackage" type="UM_SERVER.Packet.InPacket.GetGroupPacket">
        ///     <para>
        ///         获取分组信息请求包
        ///     </para>
        /// </param>
        /// <param name="DIC_ITEMINFO" type="System.Collections.Generic.Dictionary<int,string>">
        ///     <para>
        ///         用户分组信息字典
        ///     </para>
        /// </param>
        public static void ACK_SucessfulGetItemInfo(GetGroupPacket inpackage, Dictionary<int,String> DIC_ITEMINFO)
        {
            PackageBody = "";
            String ItemId = "";
            String ItemName = "";
            foreach (KeyValuePair<int, string> pair in DIC_ITEMINFO)
            {
                ItemId += pair.Key + ",";
                ItemName += pair.Value + ",";
            }
            byte[] Username = Encoding.Default.GetBytes(inpackage.GetUsername());
            byte[] UsernameLength = BitConverter.GetBytes(Username.Length);
            byte[] itemId = Encoding.Default.GetBytes(ItemId);
            byte[] ItemIdLength = BitConverter.GetBytes(ItemId.Length);
            byte[] itemName = Encoding.Default.GetBytes(ItemName);
            byte[] ItemNameLength = BitConverter.GetBytes(ItemName.Length);
            byte[] packagebody = new byte[Username.Length + itemId.Length + itemName.Length + 12];
            int Offset = 0;
            //复制用户名长度
            Array.ConstrainedCopy(UsernameLength, 0, packagebody, Offset, 4);
            Offset += 4;
            //复制用户名
            Array.ConstrainedCopy(Username, 0, packagebody, Offset, Username.Length);
            Offset += Username.Length;
            //复制ITEMID长度
            Array.ConstrainedCopy(ItemIdLength, 0, packagebody, Offset, 4);
            Offset += 4;
            //复制ITEMID
            Array.ConstrainedCopy(itemId, 0, packagebody, Offset, itemId.Length);
            Offset += itemId.Length;
            //复制ITEMNAME长度
            Array.ConstrainedCopy(ItemNameLength, 0, packagebody, Offset, 4);
            Offset += 4;
            //复制ITEMNAME
            Array.ConstrainedCopy(itemName, 0, packagebody, Offset, itemName.Length);
            Offset += itemName.Length;

            //得到协议
            byte[] protocol1 = ProtocolFactory.ProtocolFactory.GetProtocol("ReplyGetGroupInfo",
                                                                          (int)Packet.Protocol.OUT_NetworkCommand.REPLY_GET_GROUPINFO_PACKAGE,
                                                                          (int)Packet.Protocol.NetworkProtocol.Base,
                                                                          packagebody,
                                                                          ProtocolFactory.ProtocolFactory.ACK.No,
                                                                          ProtocolFactory.ProtocolFactory.ENCRYPT.No,"");
            NetControl.Send(protocol1, inpackage.GetTalkPort(), inpackage.GetUserIpAddress());
        }

        /// <summary>
        ///     回馈获取指定分组内好友请求包
        /// </summary>
        /// <param name="inpackage" type="UM_SERVER.Packet.InPacket.GetFriendByGroupPacket">
        ///     <para>
        ///         获取指定分组内好友请求包
        ///     </para>
        /// </param>
        /// <param name="FriendId" type="string[]">
        ///     <para>
        ///         查询得到的好友ID集合
        ///     </para>
        /// </param>
        public static void ACK_SucessfulGetFriendIdByGroupId(GetFriendByGroupPacket inpackage, Dictionary<String, String> DIC_FRIENDINFO)
        {
            PackageBody = "";
            byte[] packagebody = null;
            if (DIC_FRIENDINFO != null)
            {
                //该分组下存在好友
                String Friends    = "";
                String FriendsId = "";
                foreach (KeyValuePair<string, string> pair in DIC_FRIENDINFO)
                {
                    Friends += pair.Value + ",";
                    FriendsId += pair.Key + ",";
                }
                byte[] Username = Encoding.Default.GetBytes(inpackage.GetUsername());
                byte[] UsernameLength = BitConverter.GetBytes(Username.Length);
                byte[] ItemId = BitConverter.GetBytes(int.Parse(inpackage.GetItemId()));
                byte[] HasFriend = BitConverter.GetBytes(true);
                byte[] friends = Encoding.Default.GetBytes(Friends);
                byte[] friendsLength = BitConverter.GetBytes(friends.Length);
                byte[] friendsId = Encoding.Default.GetBytes(FriendsId);
                byte[] friendsIdLength = BitConverter.GetBytes(friendsId.Length);
                packagebody = new byte[Username.Length + friends.Length + friendsId.Length + 17];
                int Offset = 0;
                //复制用户名长度
                Array.ConstrainedCopy(UsernameLength, 0, packagebody, Offset, 4);
                Offset += 4;
                //复制用户名
                Array.ConstrainedCopy(Username, 0, packagebody, Offset, Username.Length);
                Offset += Username.Length;
                //复制分组ID
                Array.ConstrainedCopy(ItemId, 0, packagebody, Offset, 4);
                Offset += 4;
                //复制拥有好友的标志位
                Array.ConstrainedCopy(HasFriend, 0, packagebody, Offset, 1);
                Offset += 1;
                //复制好友ID长度
                Array.ConstrainedCopy(friendsIdLength, 0, packagebody, Offset, 4);
                Offset += 4;
                //复制好友ID
                Array.ConstrainedCopy(friendsId, 0, packagebody, Offset, friendsId.Length);
                Offset += friendsId.Length;
                //复制好友长度
                Array.ConstrainedCopy(friendsLength, 0, packagebody, Offset, 4);
                Offset += 4;
                //复制好友
                Array.ConstrainedCopy(friends, 0, packagebody, Offset, friends.Length);
                Offset += friends.Length;
            }
            else
            {
                byte[] Username = Encoding.Default.GetBytes(inpackage.GetUsername());
                byte[] UsernameLength = BitConverter.GetBytes(Username.Length);
                byte[] ItemId = BitConverter.GetBytes(int.Parse(inpackage.GetItemId()));
                byte[] HasFriend = BitConverter.GetBytes(false);
                packagebody = new byte[Username.Length + 9];
                int Offset = 0;
                //复制用户名长度
                Array.ConstrainedCopy(UsernameLength, 0, packagebody, Offset, 4);
                Offset += 4;
                //复制用户名
                Array.ConstrainedCopy(Username, 0, packagebody, Offset, Username.Length);
                Offset += Username.Length;
                //复制分组ID
                Array.ConstrainedCopy(ItemId, 0, packagebody, Offset, 4);
                Offset += 4;
                //复制拥有好友的标志位
                Array.ConstrainedCopy(HasFriend, 0, packagebody, Offset, 1);
                Offset += 1;
            }
            //得到协议
            byte[] protocol1 = ProtocolFactory.ProtocolFactory.GetProtocol("ReplyGetFriendInfoByItemId",
                                                                          (int)Packet.Protocol.OUT_NetworkCommand.REPLY_GET_FRIENDINFO_BY_GROUPID_PACKAGE,
                                                                          (int)Packet.Protocol.NetworkProtocol.Base,
                                                                          packagebody,
                                                                          ProtocolFactory.ProtocolFactory.ACK.No,
                                                                          ProtocolFactory.ProtocolFactory.ENCRYPT.No, "");
            NetControl.Send(protocol1, inpackage.GetTalkPort(), inpackage.GetUserIpAddress());
        }

        /// <summary>
        ///     回馈获取指定好友信息请求包
        /// </summary>
        /// <param name="inpackage" type="UM_SERVER.Packet.InPacket.GetFriendInfoPacket">
        ///     <para>
        ///         获取指定好友信息请求包
        ///     </para>
        /// </param>
        /// <param name="DIC_FRIENDINFO" type="System.Collections.Generic.Dictionary<string,string>">
        ///     <para>
        ///         好友信息字典
        ///     </para>
        /// </param>
        public static void ACK_SucessfulGetFriendInfo(GetFriendInfoPacket inpackage, Dictionary<String, String> DIC_FRIENDINFO)
        {
            PackageBody = "";
            int TotalSize = 0;
            if (inpackage != null && DIC_FRIENDINFO != null)
            {
                foreach (KeyValuePair<string, string> pair in DIC_FRIENDINFO)
                {
                    if (pair.Key != "PASSWORD")
                    {
                        if (pair.Key == "ONLINESTATE")
                        {
                            TotalSize += 1;
                        }
                        else if (pair.Key != "SEX" && pair.Key != "AGE" && pair.Key != "USERID" && pair.Key != "MESSION" && pair.Key != "LOGINLIVE" && pair.Key != "TALKPORT" && pair.Key != "VIDEOPORT" && pair.Key != "VOICEPORT" && pair.Key != "FILEPORT")
                        {
                            TotalSize += 4 + Encoding.Default.GetBytes(pair.Value).Length;
                        }
                        else
                        {
                            TotalSize += 4;
                        }
                    }
                }
                byte[] Usernames = Encoding.Default.GetBytes(inpackage.GetUsername());
                byte[] UsernamesLength = BitConverter.GetBytes(Usernames.Length);
                byte[] packageBody = new byte[TotalSize + 4 + Usernames.Length];
                int Offset = 0;
                foreach (KeyValuePair<string, string> pair in DIC_FRIENDINFO)
                {
                    if (pair.Key != "PASSWORD")
                    {
                        if (pair.Key == "ONLINESTATE")
                        {
                            byte[] OnlineState = BitConverter.GetBytes(bool.Parse(pair.Value.Replace("\"", "")));
                            Array.ConstrainedCopy(OnlineState, 0, packageBody, Offset, 1);
                            Offset += 1;
                        }
                        else if (pair.Key != "SEX" && pair.Key != "AGE" && pair.Key != "USERID" && pair.Key != "MESSION" && pair.Key != "LOGINLIVE" && pair.Key != "TALKPORT" && pair.Key != "VIDEOPORT" && pair.Key != "VOICEPORT" && pair.Key != "FILEPORT")
                        {
                            byte[] IntLegnth = BitConverter.GetBytes(Encoding.Default.GetBytes(pair.Value).Length);
                            byte[] Value = Encoding.Default.GetBytes(pair.Value);
                            Array.ConstrainedCopy(IntLegnth, 0, packageBody, Offset, 4);
                            Offset += 4;
                            Array.ConstrainedCopy(Value, 0, packageBody, Offset, Value.Length);
                            Offset += Value.Length;
                        }
                        else
                        {
                            byte[] IntLegnth = BitConverter.GetBytes(int.Parse(pair.Value));
                            Array.ConstrainedCopy(IntLegnth, 0, packageBody, Offset, 4);
                            Offset += 4;
                        }
                    }
                }
                Array.ConstrainedCopy(UsernamesLength, 0, packageBody, Offset, 4);
                Offset += 4;
                Array.ConstrainedCopy(Usernames, 0, packageBody, Offset, Usernames.Length);
                Offset += Usernames.Length;
                //得到协议
                byte[] protocol1 = ProtocolFactory.ProtocolFactory.GetProtocol("ReplyGetFriendInfo",
                                                                              (int)Packet.Protocol.OUT_NetworkCommand.REPLY_GET_FRIEND_INFO_PACKAGE,
                                                                              (int)Packet.Protocol.NetworkProtocol.Base,
                                                                              packageBody,
                                                                              ProtocolFactory.ProtocolFactory.ACK.No,
                                                                              ProtocolFactory.ProtocolFactory.ENCRYPT.No, "");
                NetControl.Send(protocol1, inpackage.GetTalkPort(), inpackage.GetUserIpAddress());
            }
        }
    }
}
